<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/12/20
 * Time: 18:57
 */

namespace app\index\controller;

use app\index\base_controller\BaseController;
use app\index\table\StockTypeTable;
use think\Db;
use think\Exception;

class StockType extends BaseController
{
    public function stockTypeListPage() {
        return view('stock_type_list');
    }

    public function getStockTypeList() {
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'TYPE_ID DESC';
        }

        $list = Db::table('DICT_STOCK_TYPE')
            ->order($sortStr)
            ->limit($offset, $limit)
            ->select();
        $total = Db::table('DICT_STOCK_TYPE')
            ->count();
        return ['rows' => $list, 'total' => $total];
    }

    public function addPage() {
        $this->view->engine->layout('layout_popup');
        return view('stock_type_add');
    }

    public function add() {
        $data = input();
        try{
            StockTypeTable::add($data);
            return json_encode(['code'=>'200','msg'=>'新增成功']);
        } catch (Exception $e) {
            return json_encode(['code'=>'0','msg'=>'操作数据库错误'.$e->getMessage()]);
        }
    }


    public function editPage() {
        $typeId = input('typeId');
        $condition['TYPE_ID'] = $typeId;

        $data = Db::table('DICT_STOCK_TYPE')
            ->where($condition)
            ->find();

        if (empty($data)) {
            return '查询库存分类失败';
        }

        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('stock_type_edit');
    }

    public function edit() {
        $data = input();
        $typeId = input('TYPE_ID');
        $condition['TYPE_ID'] = $typeId;

        try{
            StockTypeTable::update($condition,$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        } catch (Exception $e) {
            return json_encode(['code'=>'0','msg'=>'操作数据库错误'.$e->getMessage()]);
        }
    }

    /**
     * action 删除
     */
    public function deleteAll(){
        $list = input('list');
        Db::startTrans();
        try{
            foreach ($list as $typeId){
                Db::table('DICT_STOCK_TYPE')->where(['TYPE_ID'=>$typeId])->delete();
            }
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }


    /**
     * page 导出Excel
     */
    public function exportExcel(){
        $sortStr = 'TYPE_ID ASC';
        $condition = null;

        try{
            $list = StockTypeTable::table()->where($condition)->order($sortStr)->select();
            StockTypeTable::exportExcel($list,'库存分类');
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page 导入页面
     */
    public function importPage() {
        $this->view->engine->layout('layout_popup');
        return view('import');
    }

    /**
     * action 导入
     */

    public function import() {
        $inputData = input('data');
        $postData = base64_decode($inputData);
        $list = json_decode($postData,true);
        $start = input('start');

        Db::startTrans();
        $fieldList = StockTypeTable::STOCK_TYPE_FIELD_LIST;
        $fieldCount = count($fieldList);
        foreach ($list as $key => $row) {
            $data = [];

            // 字段赋值
            $i = 0;
            foreach ($row as $key1=>$value) {
                $data[$fieldList[$i]] = $value;
                $i++;
                if ($i>=$fieldCount) {
                    break;
                }
            }

            try{
                StockTypeTable::addImport($data);
            }
            catch (Exception $e){
                Db::rollback();
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错",'data'=>$e->getMessage()]);
            }
        }

        Db::commit();
        return json_encode(['code'=>'200','msg'=>'导入成功']);
    }





}